package com.feng.fengmall.cart.controller;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.feng.fengmall.cart.service.CartService;
import com.feng.fengmall.cart.vo.Cart;
import com.feng.fengmall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 删除购物车订单项所有商品
     * @param skuId
     * @return
     */
    @PostMapping("/deleteOrderCartItem")
    @ResponseBody
    public R deleteOrderCartItem(@RequestBody List<Long> skuId){
        cartService.deleteOrderCartItem(skuId);
        return R.ok();
    }

    @GetMapping("/{userId}/userCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems(@PathVariable("userId")Long userId){
        return cartService.getUserCartItems(userId);
    }

    @GetMapping(value = "/userCartItems2")
    @ResponseBody
    public List<CartItem> getUserCartItems2(){
        return cartService.getUserCartItems2();
    }

    /**
     * 删除数据库商品信息
     * @param session
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(HttpSession session,
                            @RequestParam("skuId") Long skuId){
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();
        System.out.println(userId);
        cartService.deleteItem(userId,skuId);

        return "redirect:http://localhost:40000/cart.html";
    }

    /**
     * 增减购物车商品数量
     * @param session
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(HttpSession session,
                            @RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();
        cartService.countItem(userId,skuId,num);

        return "redirect:http://localhost:40000/cart.html";
    }

    /**
     * 改变选中情况
     * @param session
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(HttpSession session,
                            @RequestParam Long skuId,
                            @RequestParam int check){
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();
        cartService.checkItem(userId,skuId,check);

        return "redirect:http://localhost:40000/cart.html";
    }

    /**
     * 获取购物车
     * @param session
     * @param model
     * @return
     */
    @GetMapping({"/cart.html"})
    public String cartPage(HttpSession session,Model model) {
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();
        Cart cart=cartService.getCart(userId);
        model.addAttribute("item",cart);
        return "cartList";
    }

    @GetMapping("/success.html")
    public String successPage(HttpSession session) {
        return "success";
    }

    /**
     * 添加商品信息到购物车Redis中
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            Model model,
                            HttpSession session,
                            RedirectAttributes re) throws ExecutionException, InterruptedException {
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();

        cartService.addToCart(skuId,num,userId);
        re.addAttribute("skuId",skuId);

        return "redirect:http://localhost:40000/addCartSuccess";
    }

    /**
     * 到Redis中取出加购商品信息
     * @param session
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addCartSuccess")
    public String addCartSuccess(HttpSession session,@RequestParam("skuId") Long skuId,Model model){
        MemberRespVo attribute = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        //用户id
        Long userId=attribute.getId();

        CartItem item=cartService.addCartSuccess(skuId,userId);
        model.addAttribute("item",item);
        return "success";
    }
}